<?php
/**
 * Created by PhpStorm.
 * User: liuyu
 * Date: 2018/6/12
 * Time: 11:46
 */

namespace App\Models\Api;

use App\Exceptions\ApiException;
use App\Models\Api\Base;
use App\Models\Api\Orders;
use AstroTool\Cache\CacheManage;
use Cache;
use Illuminate\Database\Eloquent\Model;

class Recode extends Base
{
    protected $table   = 'recode';
    public $timestamps = false;

    public function cat()
    {
        return $this->belongsToMany('Cat', 'recode_cat', 'cid', 'rid');
    }
    
     /**
     * 获取自己档案-走缓存
     * @param array $map
     * @return array
     * @throws Exception
     */
    public function getSelfRecode($map = [])
    {
        return (new \App\Models\CacheModel\Recode())->getRecodeLists($map['uid'], 1);
    }

     /**
     * 获取档案信息
     * @param array $map
     * id int must 档案id
     * uid int  用户id
     * @return array|bool|mixed
     * @throws Exception
     */
    public function getRecode($map = [])
    {
        $data = (new \App\Models\CacheModel\Recode())->getInfoById($map['id']);

        if (isset($map['uid'], $data['uid']) && $data['uid'] != $map['uid'])
        {
           \Log::write('getRecode：data-uid=' . $data['uid'] . ' ===== map-uid=' . $map['uid'], 'error', true);
            throw new ApiException('档案不存在');
        }
        return $data;
    }

    /**
     * 获取档案信息【用于占星师平台-星盘接口】
     * @param $rid
     * @return mixed
     * @throws Exception
     */
    public function getRecodeWithRid($rid)
    {
        return (new \App\Models\CacheModel\Recode())->getInfoById($rid);
    }

    /**
     * 获取带日月升的用户信息
     * @param array $map ['id'=> 档案id]
     * @return array
     * @throws ApiException
     * @throws Exception
     */
    public function getRecodeWithAstro($map = [])
    {
        $recode = (new \App\Models\CacheModel\Recode())->getInfoById($map['id']);
        try
        {
            $field ="'sun','moon','asc'";// 获取日月升信息

            $recoe_astro = $this->table('recode_astro')->where(['rid' => $map['id']])->first($field)->toArray();

            Cache::set('recode_astro_' . $map['id'], $recoe_astro, 86400 * 1);

        }
        catch (Exception $e)
        {
            // 错误信息写入日志
            \Log::write($e->getMessage(), 'recode_error', true);

            throw new ApiException('档案信息获取失败');
        }

        return array_merge($recode, $recoe_astro);
    }

    /**
     * 检测当前档案是否 有已支付的订单
     * @param array $map
     * id int 档案id
     * uid int 用户id
     * @return bool
     */
    public function checkAboutOrders($map = [])
    {
        $rid = $map['id'];// 档案id
        $uid = $map['uid'];// 用户id

        // 数据查询封装
        // 检测是否有生时校正 -1 已删除 ||  0未支付  ||  1等待矫正  || 2矫正成功  ||3矫正失败  ||  4 支付失败 || 5支付关闭 || 6退款成功
//        try
//        {
//            $status = $this->table('wechat.app_rectification')
//                ->field('id')
//                ->where('rid', $rid)
//                ->where('status', 'in', [1, 2, 3])
//                ->find();
//        }
//        catch (\Exception $e)
//        {
//
//            \think\Log::write('获取生时校正订单信息错误' . $e->getMessage() . var_export(request()->param(), true), true);
//            $status = [];
//        }
        $ordersModel = new Orders();
        //检测当前档案是否有生时校正订单
        $status = $ordersModel->rec_orders(['uid' => $uid, 'rid' => $rid]);
        // 检测当前档案是否有日运订单
        $dstatus = $ordersModel->fortune_orders(['uid' => $uid, 'rid' => $rid, 'type' => 1]);
        // 检测当前档案是否有月运订单
        $mstatus = $ordersModel->fortune_orders(['uid' => $uid, 'rid' => $rid, 'type' => 2]);
        // 检测当前档案是否有年运订单
        $ystatus = $ordersModel->fortune_orders(['uid' => $uid, 'rid' => $rid, 'type' => 3]);
        // 检测当前档案是否有占星地图订单
        $astatus = $ordersModel->acg_map_orders(['uid' => $uid, 'rid' => $rid]);
        if ($status || $dstatus || $mstatus || $ystatus || $astatus) return true;
        return false;
    }
}